/*
 * Toedo
 * Copyright (C) 2009-2010 J. Devauchelle and contributors.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 3 as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 */

package org.toedo.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import org.toedo.controllers.ParserController;
import org.toedo.model.comparators.AttachmentComparator;

import com.thoughtworks.xstream.annotations.XStreamOmitField;

public abstract class BaseNote extends XStreamSerializableObject {
	
	private UUID mId;
	private String mTitle;
	protected String mContent;
	protected List<Tag> mTagsList;
	private Map<UUID, Attachment> mAttachmentsMap;
	private Date mCreationDate;
	
	private boolean mIsViewOpen;
	
	@XStreamOmitField
	protected boolean mNeedContentRefresh = true;
	@XStreamOmitField
	private String mHtmlContent;
	
	public BaseNote(String title) {
		mId = UUID.randomUUID();
		mTitle = title;
		mContent = ""; //$NON-NLS-1$
		mCreationDate = new Date();
		
		mTagsList = new ArrayList<Tag>();
		mAttachmentsMap = new HashMap<UUID, Attachment>();
		
		mIsViewOpen = false;
	}
	
	@Override
	protected Object readResolve() {
		if (mCreationDate == null) {
			mCreationDate = new Date();
		}
		
		if (mTagsList == null) {
			mTagsList = new ArrayList<Tag>();
		}
		
		if (mAttachmentsMap == null) {
			mAttachmentsMap = new HashMap<UUID, Attachment>();
		}
		
		return this;
	}
	
	public UUID getId() {
		return mId;
	}
	
	public String getTitle() {
		return mTitle;
	}
	
	public void setTitle(String value) {
		mTitle = value;
	}
	
	public String getContent() {
		return mContent;
	}
	
	public void setContent(String value) {
		mContent = value;
		mNeedContentRefresh = true;
	}
	
	
	public Date getCreationDate() {
		return mCreationDate;
	}
	
	public Map<UUID, Attachment> getAttachmentsMap() {
		return mAttachmentsMap;
	}
	
	public List<Attachment> getAttachmentList() {
		List<Attachment> result = new ArrayList<Attachment>();
		
		for (Attachment attachment : mAttachmentsMap.values()) {
			result.add(attachment);
		}
		
		Collections.sort(result, new AttachmentComparator());
		
		return result;
	}
	
	public Attachment getAttachmentById(UUID id) {
		return mAttachmentsMap.get(id);
	}
	
	public List<Tag> getTagsList() {
		return mTagsList;
	}
	
	public void setNeedContentRefresh() {
		mNeedContentRefresh = true;
	}
	
	private void processContent() {
		mHtmlContent = ParserController.getInstance().getHtml(mContent);
		mNeedContentRefresh = false;		
	}
	
	public String getHtmlContent() {
		if (mNeedContentRefresh) {
			processContent();
		}
		return mHtmlContent;
	}
	
	public boolean isViewOpen() {
		return mIsViewOpen;
	}
	
	public void setViewOpen(boolean value) {
		mIsViewOpen = value;
	}
	
	protected abstract void addTag(Tag tag);
	
	protected abstract void removeTag(Tag tag);
}
